import requests
from selenium import webdriver
import time

# 应用账号（请替换为真实账号）
app_key = 'app_key'
# 应用密码（请替换为真实密码）
app_secret = 'app_secret'

api_url = "https://api.xiaoxiangdaili.com/ip/get"

def getProxy():
    res = requests.get(api_url, params={'appKey': app_key, 'appSecret': app_secret, 'wt': 'text', 'cnt': 1})
    content = str(res.content,'utf-8')
    print("API response: " + content)
    return content


fp = webdriver.FirefoxProfile()
proxy = getProxy()
ip, port = proxy.split(":")
port = int(port)

# 设置代理配置
fp.set_preference('network.proxy.type', 1)
fp.set_preference('network.proxy.http', ip)
fp.set_preference('network.proxy.http_port', port)
fp.set_preference('network.proxy.ssl', ip)
fp.set_preference('network.proxy.ssl_port', port)

driver = webdriver.Firefox(executable_path="#geckodriver驱动存放路径#", firefox_profile=fp)
driver.get("http://httpbin.org/ip")

# 获取页面内容
print(driver.page_source)

# 延迟3秒后关闭当前窗口，如果是最后一个窗口则退出
time.sleep(3)
driver.close()